package com.chwn.curiousdiary.ui.story.presenter;

import android.text.TextUtils;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.api.IHomeApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.ui.story.contract.NewCommendContract;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.mvp.rx.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;

/**
 * Created by Myshikin on 2017/3/20.
 */
public class NewCommendPresenter extends NewCommendContract.Presenter {

    private Observer<HttpResult<List<StoryBean>>> homeListObserver = new Observer<HttpResult<List<StoryBean>>>() {
        @Override
        public void onCompleted() {
            mView.onRequestSuccess();
        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<StoryBean>> httpResult) {
            if (httpResult.getResult() == 1) {
                List<StoryBean> data = httpResult.getResdata();
                mView.showNewCommendList(data);
            } else {
                mView.onRequestError(new HttpResultException(2, "数据为空"));
            }
        }
    };


    @Override
    public void getNewCommendList(String storyPackageId, int page) {
        User user = DiaryApplication.mInstance.getmUser();
        String userToken = DiaryApplication.mInstance.getUserToken();

        int sex = 1;
        String babybirth = "1330531200";

        if (user != null) {
            sex = Integer.parseInt(user.getBabysex());
            babybirth = user.getBabybirth();
        }
        if (!TextUtils.isEmpty(storyPackageId)) {
            NetWork.getInstance()
                    .getApiService(IHomeApi.class, true)
                    .getStoryList(storyPackageId, userToken, Constant.PAGE_COUNT, page, Constant.OSTYPE)
                    .compose(RxSchedulers.<HttpResult<List<StoryBean>>>io_main())
                    .subscribe(homeListObserver);
        } else {
            /*NetWork.getInstance()
                    .getApiService(IHomeApi.class, true)
                    .getNewestStoryList(Constant.PAGE_COUNT, page, Constant.OSTYPE)
                    .compose(RxSchedulers.<HttpResult<List<StoryBean>>>io_main())
                    .subscribe(homeListObserver);*/
        }

        //         test();
    }

    private void test() {
        ArrayList<StoryBean> storyList = new ArrayList<>();
        storyList.add(new StoryBean("001", "故事1", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/Fhqmunc5wT5ttBz_zVfZt9tDJCjH?imageView2/1/w/1000/h/2000", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_6_3_1_Video_90765034.mp3", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_6_3_1_Video_90765034.mp3"));
        storyList.add(new StoryBean("002", "故事2", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/8bf43ffd9d184fb48b89036b1b08039a.jpg", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_6_3_1_Video_90765034.mp3", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_6_3_1_Video_90765034.mp3"));
        storyList.add(new StoryBean("003", "故事3", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/BPOKKEJJFBJKNFKF", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_7_3_34_Video_71289722.mp3", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_7_3_34_Video_71289722.mp3"));
        storyList.add(new StoryBean("004", "故事4", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/FmalYkxFyETumripqhyASKR3lsoM?imageView2/1/w/1000/h/2000", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_7_2_17_Video_4853543.mp3", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_7_2_17_Video_4853543.mp3"));
        storyList.add(new StoryBean("005", "故事5", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/f82470b8e5044e3a85474aa8f3cde3b9.jpg", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_6_4_9_Video_6776522.mp3", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_6_4_9_Video_6776522.mp3"));
        storyList.add(new StoryBean("006", "故事6", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/fc125ee4c3954f88b18ebbc7f01809b5.jpg", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_7_2_42_Video_31102223.mp3", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_3_7_2_42_Video_31102223.mp3"));
        storyList.add(new StoryBean("007", "故事7", "故事内容", "http://ohqtfu1dy.bkt.clouddn.com/fc125ee4c3954f88b18ebbc7f01809b5.jpg", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_03_06_15_40_53_441_img_433e6889-f830-4587-ad42-fcbed454cc57.wav", "http://xclass-project.oss-cn-shanghai.aliyuncs.com/appvoice/2017_03_06_15_40_53_441_img_433e6889-f830-4587-ad42-fcbed454cc57.wav"));


        mView.showNewCommendList(storyList);
    }
}
